package com.oceansoft.core.dao.impl;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import com.oceansoft.core.common.util.DatetimeUtil;
import com.oceansoft.core.dao.IAccessoryDao;
import com.oceansoft.core.domain.Accessory;

@Repository
public class AccessoryDaoImpl implements IAccessoryDao {

	
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Resource
	private LobHandler lobHandler;

	@Resource
	private NamedParameterJdbcTemplate npjt;

	@Override
	public void uploadFile(final Accessory accessory,MultipartFile file) {
		try {
			accessory.setFjnr(file.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		StringBuffer sql = new StringBuffer(
				"insert into t_sacw_fj(dbid,fjbh,fjly,fjlx,fjmc,fjnr,djr,djrxm,djsj,xgsj,sfsc");
		sql.append(") values(?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),to_date(?,'yyyy-mm-dd hh24:mi:ss'),?)");
		jdbcTemplate.execute(sql.toString(),
				new AbstractLobCreatingPreparedStatementCallback(
						this.lobHandler) {
					@Override
					protected void setValues(PreparedStatement ps,
							LobCreator lobCreator) throws SQLException,
							DataAccessException {
						ps.setInt(1, accessory.getDbid());
						ps.setString(2, accessory.getFjbh());
						ps.setString(3, accessory.getFjly());
						ps.setString(4, accessory.getFjlx());
						ps.setString(5, accessory.getFjmc());
						lobCreator.setBlobAsBytes(ps, 6, accessory.getFjnr());
						ps.setString(7, accessory.getDjr());
						ps.setString(8, accessory.getDjrxm());
						ps.setString(9, DatetimeUtil.formatDate(accessory.getDjsj(), "yyyy-MM-dd HH:mm:ss"));
						ps.setString(10, DatetimeUtil.formatDate(accessory.getXgsj(), "yyyy-MM-dd HH:mm:ss"));
						ps.setString(11, accessory.getSfsc());
					}
				});
	}
	
	@Override
	public int delete(String accessoryId){
		String sql = "delete from t_sacw_fj where fjbh=?";
		return jdbcTemplate.update(sql, accessoryId);
	}
	
	@Override
	public Accessory findById(String accessoryId){
		String sql = "SELECT FJBH,WPBH,FJLY,FJLX,FJMC,FJNR,DJR,DJRXM,DJSJ,XGSJ,SFSC FROM T_SACW_FJ WHERE FJBH=?";
		List<Accessory> list = jdbcTemplate.query(sql,
				new RowMapper<Accessory>() {
					@Override
					public Accessory mapRow(ResultSet rs, int arg1)
							throws SQLException {
						Accessory accessory = new Accessory();
						accessory.setFjbh(rs.getString("fjbh"));
						accessory.setFjly(rs.getString("fjly"));
						accessory.setFjlx(rs.getString("fjlx"));
						accessory.setFjmc(rs.getString("fjmc"));
						accessory.setFjnr(lobHandler.getBlobAsBytes(rs,
								"fjnr"));
						accessory.setDjr(rs.getString("djr"));
						accessory.setDjrxm(rs.getString("djrxm"));
						accessory.setDjsj(rs.getDate("djsj"));
						accessory.setXgsj(rs.getDate("xgsj"));
						accessory.setSfsc(rs.getString("sfsc"));
						return accessory;
					}
				}, accessoryId);
		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
